package com.tiancheng.trade.commom.mq.rocket.autoconfigure;

import com.tiancheng.trade.commom.mq.rocket.RocketEnhanceProperties;
import org.apache.rocketmq.spring.support.DefaultRocketMQListenerContainer;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;
import org.springframework.util.StringUtils;

/**
 * @Author: likailun
 * @Description:
 * @Date: create in 2024/9/20 14:14
 */
public class EnvironmentIsolationConfig implements BeanPostProcessor {
	private RocketEnhanceProperties rocketEnhanceProperties;

	public EnvironmentIsolationConfig(RocketEnhanceProperties rocketEnhanceProperties) {
		this.rocketEnhanceProperties = rocketEnhanceProperties;
	}


	/**
	 * 在装载Bean之前实现参数修改
	 */
	@Override
	public Object postProcessBeforeInitialization(Object bean, String beanName) throws BeansException {
		if(bean instanceof DefaultRocketMQListenerContainer){

			DefaultRocketMQListenerContainer container = (DefaultRocketMQListenerContainer) bean;

			if(rocketEnhanceProperties.isEnabledIsolation() && StringUtils.hasText(rocketEnhanceProperties.getEnvironment())){
				container.setTopic(String.join("_", container.getTopic(),rocketEnhanceProperties.getEnvironment()));
			}
			return container;
		}
		return bean;
	}
}
